<template>
  <div class="resident-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>住户信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="住户姓名">
        <el-input v-model="searchParams.name" placeholder="请输入住户姓名" maxlength="64"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="searchParams.phone" placeholder="请输入联系电话" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="楼栋名称" prop="buildingName">
        <el-input v-model="searchParams.buildingName" placeholder="请选择楼栋名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findBuilding"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="单元房号" prop="roomNumber">
        <el-input v-model="searchParams.roomNumber" placeholder="请选择单元房号" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findRoom"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="住户类型" prop="type">
        <el-select v-model="searchParams.type" placeholder="请选择住户类型">
          <el-option label="业主" :value="10"></el-option>
          <el-option label="成员" :value="20"></el-option>
          <el-option label="租户" :value="30"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="name" label="住户姓名"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="150"></el-table-column>
      <el-table-column prop="buildingName" label="楼栋名称"></el-table-column>
      <el-table-column prop="roomNumber" label="单元房号"></el-table-column>
      <el-table-column prop="moveInDate" label="入驻日期"></el-table-column>
      <el-table-column prop="moveOutDate" label="搬出日期"></el-table-column>
      <el-table-column prop="type" label="住户类型">
        <template v-slot="{ row }">
          <div v-if="row.type === 10">业主</div>
          <div v-if="row.type === 20">成员</div>
          <div v-if="row.type === 30">租户</div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="buildingSelectorVisible" v-if="buildingSelectorVisible" title="楼栋信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth">
      <building-selector @building-selected-event="handleBuildingSelectedEvent">
      </building-selector>
    </el-dialog>
    <el-dialog :visible.sync="roomSelectorVisible" v-if="roomSelectorVisible" title="单元信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <room-selector @room-selected-event="handleRoomSelectedEvent">
      </room-selector>
    </el-dialog>
    <el-dialog :visible.sync="residentAddVisible" v-if="residentAddVisible" :title="residentAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <resident-add :id="selectedResidentId" @close-resident-add-event="handleCloseResidentAddEvent" @reset-resident-add-event="handleResetResidentAddEvent">
      </resident-add>
    </el-dialog>
    <el-dialog :visible.sync="residentViewVisible" v-if="residentViewVisible" title="住户信息详情" :top="dialogTop" :width="dialogWidth">
      <resident-view :id="selectedResidentId" @close-resident-view-event="handleCloseResidentViewEvent">
      </resident-view>
    </el-dialog>
  </div>
</template>

<script>
import residentApi from '@/api/residentApi'
import BuildingSelector from "@/views/building/BuildingSelector.vue";
import RoomSelector from "@/views/room/RoomSelector.vue";
import ResidentAdd from "@/views/resident/ResidentAdd.vue"
import ResidentView from "@/views/resident/ResidentView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
import {getResidentTypeText} from "@/utils/dictTranslator";
export default {
  name: 'ResidentList',
  components: {BuildingSelector,RoomSelector,ResidentAdd, ResidentView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        name: '',
        phone: '',
        buildingId: '',
        buildingName: '',
        roomId: '',
        roomNumber: '',
        type: ''
      },
      buildingSelectorVisible: false,
      roomSelectorVisible: false,
      residentAddVisible: false,
      residentAddTitle: '',
      residentViewVisible: false,
      selectedResidentId: 0
    }
  },
  methods: {
    getPageData (p) {
      return residentApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        name: '',
        phone: '',
        buildingId: '',
        buildingName: '',
        roomId: '',
        roomNumber: '',
        type: ''
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedResidentId = ''
      this.residentAddVisible = true
      this.residentAddTitle = '住户信息新增'
    },
    onExport () {
      const headers = ['住户姓名','联系电话','电子邮箱','楼栋名称','单元房号','入驻日期','搬出日期','住户类型']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.name, d.phone, d.email, d.buildingName, d.roomNumber, d.moveInDate, d.moveOutDate, getResidentTypeText(d.type)])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedResidentId = id
      this.residentAddVisible = true
      this.residentAddTitle = '住户信息编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        residentApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedResidentId = id
      this.residentViewVisible = true
    },
    findBuilding () {
      this.buildingSelectorVisible = true
    },
    handleBuildingSelectedEvent (selectedBuilding) {
      this.buildingSelectorVisible = false
      if (selectedBuilding && 'buildingId' in selectedBuilding && this.searchParams.buildingId !== selectedBuilding['buildingId']) {
        this.searchParams.buildingId = selectedBuilding['buildingId']
        this.searchParams.buildingName = selectedBuilding['buildingName']
      }
    },
    findRoom () {
      this.roomSelectorVisible = true
    },
    handleRoomSelectedEvent (selectedRoom) {
      this.roomSelectorVisible = false
      if (selectedRoom && 'roomId' in selectedRoom && this.searchParams.roomId !== selectedRoom['roomId']) {
        this.searchParams.roomId = selectedRoom['roomId']
        this.searchParams.roomNumber = selectedRoom['roomNumber']
      }
    },
    handleCloseResidentViewEvent () {
      this.residentViewVisible = false
    },
    handleResetResidentAddEvent () {
      this.residentAddTitle = '住户信息新增'
    },
    handleCloseResidentAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.residentAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>